package com.cskaoyan.catmall.service.Impl;

import com.cskaoyan.catmall.dao.*;
import com.cskaoyan.catmall.model.bo.*;
import com.cskaoyan.catmall.model.pojo.*;
import com.cskaoyan.catmall.model.vo.*;
import com.cskaoyan.catmall.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @Author: 逍遥子
 * @Date: 2022/01/07/18:31
 * @Description:
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderDao orderDao;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;

    @Autowired
    MarketOrderMapper marketOrderMapper;

    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Autowired
    MarketCartMapper marketCartMapper;

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketGrouponMapper marketGrouponMapper;

    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    @Autowired
    MarketGrouponRulesMapper marketGrouponRulesMapper;
    /**
     * @param
     * @param orderBo
     * @return com.cskaoyan.catmall.model.vo.OrderVo
     * @description 查询所有订单
     * @author czx
     * @date 2022-01-07 19:09
     */
    @Override
    public OrderVo selectOrder(OrderBo orderBo) {

        Integer page = orderBo.getPage();
        Integer limit = orderBo.getLimit();

        PageHelper.startPage(page, limit);


        List<OrderVo.ListDTO> listDTOS = orderDao.selectOrder(orderBo);


        PageInfo<OrderVo.ListDTO> pageInfo = new PageInfo<>(listDTOS);

        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();

        return new OrderVo(total, pages, limit, page, listDTOS);


    }

    /**
     * @param
     * @param id
     * @return com.cskaoyan.catmall.model.vo.OrderDetailVo
     * @description 订单详情
     * @author czx
     * @date 2022-01-07 19:59
     */

    @Override
    public OrderDetailVo selectOrderDetailById(Integer id) {
        OrderDetailVo.OrderDTO orderDTO = orderDao.selectOrderDetailById(id);

        // 根据订单id查询下单用户id, 再根据用户id到用户表查用户信息
        List<OrderDetailVo.OrderGoodsDTO> orderGoodsDTOS = orderDao.selectOrderGood(id);


        OrderDetailVo orderDetailVo = new OrderDetailVo();

        // 封装用户信息, 接口暂时未调用, 先写死
        OrderDetailVo.UserDTO userDTO = new OrderDetailVo.UserDTO();
        userDTO.setAvatar("https://lmg.jj20.com/up/allimg/tx31/122918423730424411.jpg");
        userDTO.setNickname("xyz");

        // 封装商品详细信息
        orderDetailVo.setOrderGoods(orderGoodsDTOS);
        // 封装订单详情
        orderDetailVo.setOrder(orderDTO);
        // 封装用户信息
        orderDetailVo.setUser(userDTO);
        return orderDetailVo;
    }

    /**
     * @param
     * @param shipBo
     * @return int
     * @description 更新订单状态
     * @author czx
     * @date 2022-01-07 20:59
     */
    @Override
    public int updateOrderStatus(ShipBo shipBo) {
        System.out.println(shipBo.getOrderId());
        return orderDao.updateOrderStatus(shipBo);
    }

    /**
     * @param
     * @return java.util.List<com.cskaoyan.catmall.model.vo.Channel>
     * @description 获取快递信息
     * @author czx
     * @date 2022-01-08 10:09
     */
    @Override
    public List<ChannelVo> selectChannel() {
        return orderDao.selectChannel();
    }

    /**
     * @param
     * @param id
     * @return int
     * @description 删除订单
     * @author czx
     * @date 2022-01-08 10:38
     */
    @Override
    public int deleteOrder(Integer id) {
        return orderDao.deleteOrder(id);
    }

    /**
     * @param
     * @param showType
     * @param page
     * @param limit
     * @return com.cskaoyan.catmall.model.vo.WxOrderListVo
     * @description 小程序订单列表
     * @author ghr
     * @date 2022/1/11 4:07 下午
     */
    @Override
    public WxOrderListVo selectOrderList(Integer showType, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        PageHelper.clearPage();
        WxOrderListVo wxOrderListVo = new WxOrderListVo();


        List<WxOrderDetailVo> list = wxOrderListVo.getList();
        list = orderMapper.selectOrderList(showType);
        PageInfo<WxOrderDetailVo> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        wxOrderListVo.setTotal((int) total);
        wxOrderListVo.setPages(pages);
        wxOrderListVo.setPage(page);
        wxOrderListVo.setLimit(limit);
        wxOrderListVo.setList(list);
        return wxOrderListVo;
    }

    /**
     * @param
     * @return void
     * @description 小程序取消订单
     * @author ghr
     * @date 2022/1/11 10:28 上午
     */
    @Override
    public void cancelOrder(Integer orderId) {
        orderMapper.updateOrderStatus(orderId);
    }

    /**
     * @param
     * @param orderId
     * @return void
     * @description 小程序订单申请退款
     * @author ghr
     * @date 2022/1/11 10:48 上午
     */
    @Override
    public void refundOrder(Integer orderId) {
        orderMapper.updateOrderRefund(orderId);
    }

    /**
     * @param
     * @param orderId
     * @return void
     * @description 小程序订单确认收货
     * @author ghr
     * @date 2022/1/11 11:02 上午
     */
    @Override
    public void confirmOrder(Integer orderId) {
        orderMapper.updateOrderConfirm(orderId);
        orderMapper.updateOrderGoodsComment(orderId,0);
    }

    /**
     * @param
     * @param orderId
     * @param goodsId
     * @return com.cskaoyan.catmall.model.pojo.MarketOrderGoods
     * @description 小程序评价商品页面
     * @author ghr
     * @date 2022/1/11 11:16 上午
     */
    @Override
    public MarketOrderGoods goods(Integer orderId, Integer goodsId) {
        MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = marketOrderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        criteria.andGoodsIdEqualTo(goodsId);
        criteria.andDeletedEqualTo(false);
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(marketOrderGoodsExample);
        return marketOrderGoods.get(0);
    }

    /**
     * @param
     * @param wxCommentBo
     * @return void
     * @description 小程序评价商品
     * @author ghr
     * @date 2022/1/11 11:30 上午
     */
    @Override
    public void orderComment(WxCommentBo wxCommentBo) {
        MarketComment marketComment = new MarketComment();
        marketComment.setAddTime(new Date());
        marketComment.setUpdateTime(new Date());
        marketComment.setContent(wxCommentBo.getContent());
        marketComment.setHasPicture(wxCommentBo.getHasPicture());
        marketComment.setPicUrls(wxCommentBo.getPicUrls());
        marketComment.setStar(wxCommentBo.getStart());
        //插入返回主键
        marketCommentMapper.insertSelective(marketComment);

        MarketOrderGoods marketOrderGoods = new MarketOrderGoods();
        marketOrderGoods.setUpdateTime(new Date());
        marketOrderGoods.setComment(marketComment.getId());
        MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = marketOrderGoodsExample.createCriteria();
        criteria.andIdEqualTo(wxCommentBo.getOrderGoodsId());
        marketOrderGoodsMapper.updateByExampleSelective(marketOrderGoods, marketOrderGoodsExample);
    }

    /**
     * @description 订单细节
     * @author ghr
     * @date 2022/1/11 9:04 下午
     * @param orderId
     * @return com.cskaoyan.catmall.model.vo.WxOrderGoodsDetailVo
     */
    @Override
    public WxOrderGoodsDetailVo detail(Integer orderId) {
        WxOrderGoodsDetailVo detailVo = new WxOrderGoodsDetailVo();
        WxOrderInfo orderInfo = orderMapper.selectOrderInfo(orderId);

        List<WxOrderGoods> orderGoods = orderMapper.selectOrderGoods(orderId);
        detailVo.setOrderInfo(orderInfo);
        detailVo.setOrderGoods(orderGoods);
        return detailVo;
    }

    /**
     * @description 小程序删除订单
     * @author ghr
     * @date 2022/1/12 12:17 上午
     * @param
     * @param orderId
     * @return void
     */
    @Override
    public void deleteOrderById(Integer orderId) {
        orderMapper.deleteOrderById(orderId);
    }

    /**
     * @description
     * 退款, 涉及到支付, 后面写
     * 当后台管理人员点击退款的时候, 修改订单的状态为已退款
     * @author czx
     * @date 2022-01-09 17:10
     * @param
     * @param refundBo
     * @return int
     */
    @Override
    public int OrderRefund(RefundBo refundBo) {
        return orderDao.OrderRefund(refundBo);
    }

    /**
     * @description 小程序购物车确认订单
     * @author ghr
     * @date 2022/1/12 8:58 下午
     * @param
     * @param submitBo
     * @return com.cskaoyan.catmall.model.vo.WxOrderSubmitVo
     */
    @Override
    public WxOrderSubmitVo submit(WxOrderSubmitBo submitBo) {
        Integer addressId = submitBo.getAddressId();
        MarketAddress marketAddress = marketAddressMapper.selectByPrimaryKey(addressId);

        Integer cartId = submitBo.getCartId();
        MarketCart marketCart = marketCartMapper.selectByPrimaryKey(cartId);

        Integer couponId = submitBo.getCouponId();
        MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(couponId);

        Integer grouponLinkId = submitBo.getGrouponLinkId();
        MarketGroupon marketGroupon = marketGrouponMapper.selectByPrimaryKey(grouponLinkId);

        Integer grouponRulesId = submitBo.getGrouponRulesId();
        MarketGrouponRules marketGrouponRules = marketGrouponRulesMapper.selectByPrimaryKey(grouponRulesId);

        Integer userCouponId = submitBo.getUserCouponId();
        MarketCouponUser marketCouponUser = marketCouponUserMapper.selectByPrimaryKey(userCouponId);

        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setAddTime(new Date());
        marketOrder.setUpdateTime(new Date());
        marketOrder.setUserId(marketAddress.getUserId());
        int ran = (int)(Math.random()*100000);
        marketOrder.setOrderSn(ran+"");
        marketOrder.setOrderStatus((short) 101);
        marketOrder.setAftersaleStatus((short) 0);
        marketOrder.setConsignee(marketAddress.getName());
        marketOrder.setAddress(marketAddress.getAddressDetail());
        marketOrder.setMobile(marketAddress.getTel());
        marketOrder.setMessage(submitBo.getMessage());
        marketOrder.setGoodsPrice(marketCart.getPrice());
        marketOrder.setCouponPrice(marketCoupon.getDiscount());
        if ( marketCart.getPrice().intValue() > 88){
            marketOrder.setFreightPrice(BigDecimal.valueOf(10));
        }else {
            marketOrder.setFreightPrice(BigDecimal.valueOf(0));
        }
        marketOrder.setIntegralPrice(BigDecimal.valueOf(10));
        marketOrder.setGrouponPrice(BigDecimal.valueOf(20));
        marketOrder.setOrderPrice(marketCart.getPrice().add(marketOrder.getFreightPrice()).subtract(marketCoupon.getDiscount()));
        marketOrder.setActualPrice(marketOrder.getOrderPrice().subtract(BigDecimal.valueOf(10)));
        marketOrder.setPayId("1001");
        marketOrder.setPayTime(new Date());
        marketOrder.setShipSn("120232345");
        marketOrder.setShipChannel("顺丰快递");
        marketOrder.setShipTime(new Date());
        marketOrder.setRefundType("微信支付");
        marketOrderMapper.insertSelective(marketOrder);

        MarketOrderGoods marketOrderGoods = new MarketOrderGoods();
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
        criteria.andOrderSnEqualTo(ran+"");
        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(marketOrderExample);
        MarketOrder marketOrder1 = marketOrders.get(0);
        Integer id = marketOrder1.getId();
        marketOrderGoods.setOrderId(id);
        Integer goodsId = marketCart.getGoodsId();
        marketOrderGoods.setGoodsId(goodsId);
        String goodsName = marketCart.getGoodsName();
        marketOrderGoods.setGoodsName(goodsName);
        String goodsSn = marketCart.getGoodsSn();
        marketOrderGoods.setGoodsSn(goodsSn);
        Integer productId = marketCart.getProductId();
        marketOrderGoods.setProductId(productId);
        Short number = marketCart.getNumber();
        marketOrderGoods.setNumber(number);
        marketOrderGoods.setPrice(marketCart.getPrice());
        String specifications = marketCart.getSpecifications();
        marketOrderGoods.setSpecifications(specifications);
        marketOrderGoods.setPicUrl(marketCart.getPicUrl());
        marketOrderGoods.setAddTime(new Date());
        marketOrderGoods.setUpdateTime(new Date());
        marketOrderGoodsMapper.insertSelective(marketOrderGoods);
        //优惠券状态码
        marketCouponUser.setStatus((short) 1);
        marketCouponUserMapper.updateByPrimaryKeySelective(marketCouponUser);

        WxOrderSubmitVo submitVo = new WxOrderSubmitVo();
        submitVo.setOrderId(id);
        submitVo.setGrouponLinkId(grouponLinkId);
        return submitVo;
    }
}
